package com.zzl.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.zzl.springboot.entity.User;
import com.zzl.springboot.entity.UserVol;
import com.zzl.springboot.entity.Vol;
import com.zzl.springboot.service.VolService;
import com.zzl.springboot.utils.DataResult;
import com.zzl.springboot.utils.UpDownUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: AAAzzl
 * @description:
 * @author: zzl
 * @create: 2022-11-29 22:20
 * @version:1.0
 **/

@Api(tags = "志愿服务模块")
@Controller
@RequestMapping("/vol")
public class VolController {

    @Resource
    VolService volService;

    @ApiOperation("发送志愿服务")
    @RequestMapping("/send")
    public String send(Vol vol, MultipartFile pic, HttpServletRequest req, HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        String upload = UpDownUtil.upload(req, pic);
        vol.setVolImg(upload);
        vol.setUserId(user.getId());
        vol.setCreateTime(new Date());
        vol.setState(0);
        vol.setUserMaster(0);
        System.out.println(vol.toString());
        int i = volService.setVolInfo(vol);
        //fixme: 未做错误检查直接跳转了
        return "redirect:/user/index";
    }


    @ApiOperation("获取志愿服务数据列表")
    @RequestMapping("/getUserList")
    @ResponseBody
    public DataResult getUserList(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "20") int pageSize){

        PageInfo<Vol> volList = volService.getVolList(pageNum, pageSize);

        return DataResult.success(volList,null);
    }

    @ApiOperation("获取当前用户发送的志愿服务")
    @RequestMapping("/getVolSend")
    @ResponseBody
    public DataResult getVolSend(HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");
        List<Vol> list = volService.selectByUserId(userInfo.getId());
        System.out.println(list.toString());
        return DataResult.success(list,null);
    }

    @ApiOperation("/获取当前用户接取的志愿服务")
    @RequestMapping("/getVolGet")
    @ResponseBody
    public DataResult getVolGet(HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");
        List<UserVol> list = volService.selectVolUserByUserId(userInfo.getId());
        System.out.println(list.toString());
        return DataResult.success(list,null);
    }

    @ApiOperation("接取志愿服务")
    @RequestMapping("/getVol")
    @ResponseBody
    public DataResult getVol(@RequestBody Vol vol,HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");
        vol.setUserMaster(userInfo.getId());
        // 判断是否是接取的自己发布的志愿服务
        if (vol.getUserId().equals(userInfo.getId())){
            return DataResult.fail("请务接取自己发布的志愿服务");
        }
        UserVol userVol = volService.selectVolUser(vol.getId(), userInfo.getId());
        if (userVol!=null){
            return DataResult.fail("您已接取该志愿服务,请务重复接取!");
        }
        Integer i = volService.setVolUser(vol);
        if (i==0){
            return DataResult.fail("该志愿服务人员已满！");
        }
        System.out.println(userInfo.toString());
        System.out.println(vol.toString());
        System.out.println(i);
        return DataResult.success();
    }


    @ApiOperation("完成提交志愿服务请求")
    @RequestMapping("/commitVol")
    @ResponseBody
    public DataResult commitVol(@RequestBody Vol vol){
        System.out.println(vol.toString());
        volService.commitVol(vol.getId());
        return DataResult.success();
    }

}
